<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <style>
    div {
      width: 300px;
      height: 300px;
      background-color: blue;
      position: absolute;
      left: 0px;
      top: 0;
    }
    span {
      display: inline-block;
      width: 1px;
      height: 300px;
      background: black;
      position: absolute;
      left: 1000px;
      top: 0px;
    }
    button {
      margin-top: 400px;
    }
  </style>
  <body>
    <div></div>
    <span></span>
    <button>run</button>
    <button>run1</button>
    <script>
      var oDiv = document.getElementsByTagName('div')[0];
      var Btn = document.getElementsByTagName('button')[0];
      var Btn1 = document.getElementsByTagName('button')[1];
      var timer = null;
      Btn.onclick = function () {
        yunsu();
      };
      Btn1.onclick = function () {
        huanchong(Btn, 1000);
      };
      function huanchong(DOM, time) {
        timer = setInterval(() => {
          var speed = (time - DOM.offsetLeft) / 12;
          speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
          if (DOM.offsetLeft == time) {
            clearInterval(timer);
          } else {
            oDiv.style.left = oDiv.offsetLeft + speed + 'px';
          }
        }, 30);
      }
      function yunsu(DOM, time) {
        var speed = DOM.offsetLeft - time > 0 ? -7 : 7;
        timer = setInterval(() => {
          DOM.style.left = DOM.offsetLeft + speed + 'px';
          if (Math.abs(DOM.offsetLeft - time) < Math.abs(speed)) {
            DOM.style.left = '1000px';
            clearInterval(timer);
          }
        }, 30);
      }
    </script>
  </body>
</html>
